﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace JotaControls
{
    //TODO:Ainda não está funcional

    [ToolboxData("<{0}:CustomProgress runat=server></{0}:CustomProgress>")]
    public class CustomProgress : CompositeControl
    {
        /// <summary>
        /// Propriedade para definir o ID do Update Panel.
        /// </summary>
        [Category("CustomProperties")]
        [Description("Update Panel a utilizar.")]
        public string UpdPanelId { get; set; }

        protected override void CreateChildControls()
        {
            UpdateProgress UpdPrg = new UpdateProgress();
            Panel PnlBackgroundFilter = new Panel();
            Panel PnlProcessMessage = new Panel();
            Label lblLoading = new Label();
            Label lblL1 = new Label();
            Label lblL2 = new Label();
            Label lblL3 = new Label();
            Label lblL4 = new Label();
            Label lblL5 = new Label();
            Label lblL6 = new Label();
            PnlBackgroundFilter.ID = "progressBackgroundFilter";
            PnlProcessMessage.ID = "processMessage";
            lblLoading.Text = "Loading";
            lblL1.CssClass = "l-1";
            lblL2.CssClass = "l-2";
            lblL3.CssClass = "l-3";
            lblL4.CssClass = "l-4";
            lblL5.CssClass = "l-5";
            lblL6.CssClass = "l-6";
            PnlProcessMessage.Controls.Add(lblLoading);
            PnlProcessMessage.Controls.Add(lblL1);
            PnlProcessMessage.Controls.Add(lblL2);
            PnlProcessMessage.Controls.Add(lblL3);
            PnlProcessMessage.Controls.Add(lblL4);
            PnlProcessMessage.Controls.Add(lblL5);
            PnlProcessMessage.Controls.Add(lblL6);

            UpdPrg.Controls.Add(PnlBackgroundFilter);
            UpdPrg.Controls.Add(PnlProcessMessage);
            UpdPrg.AssociatedUpdatePanelID = UpdPanelId;
            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!DesignMode && Page.Header != null)
            {
                bool linkIncluded = false;
                foreach (Control c in Page.Header.Controls)
                {
                    if (c.ID == "CustomButtonStyle")
                    {
                        linkIncluded = true;
                    }
                }
                if (!linkIncluded)
                {
                    HtmlGenericControl csslink = new HtmlGenericControl("link");
                    csslink.ID = "CustomButtonStyle";
                    csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(CustomProgress), "JotaControls.Css.Style.css"));
                    csslink.Attributes.Add("type", "text/css");
                    csslink.Attributes.Add("rel", "stylesheet");
                    Page.Header.Controls.Add(csslink);
                }
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            this.RenderContents(output);
        }
    }
}
